package com.project.controller;

import com.project.model.vo.ReportModules;
import com.project.result.Result;
import com.project.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.project.model.entity.Module;

import java.util.List;

@RestController
@RequestMapping("/module")
public class ModuleController {
    private final ModuleService moduleService;

    @Autowired
    public ModuleController(ModuleService moduleService) {
        this.moduleService = moduleService;
    }

    @PostMapping("saveOrUpdate")
    public Result<Module> saveOrUpdateModule(@RequestBody Module module) {
        try {
            if (module == null) {
                return Result.fail("该模块为空");
            }

            if (moduleService.getModuleById(module.getId())==null) {
                Module savedModule = moduleService.addModule(module);
                return savedModule != null ? Result.ok(savedModule) : Result.fail("保存失败");
            } else {
                Module updatedModule = moduleService.updateModule(module);
                return updatedModule != null ? Result.ok(updatedModule) : Result.fail("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @DeleteMapping("delete")
    public Result<Void> deleteModule(@RequestParam Integer id) {
        if (id == null || id <= 0) {
            return Result.fail("ID值无效");
        }
        moduleService.deleteModule(id);
        return Result.ok();
    }

    @GetMapping("getById")
    public Result<Module> getModuleById(@RequestParam Integer id) {
        try {//测试
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            Module module = moduleService.getModuleById(id);
            return module != null ? Result.ok(module) : Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("getAll")
    public Result<List<Module>> getAllModules() {
        List<Module> modules = moduleService.getAllModules();
        return modules != null && !modules.isEmpty() ? Result.ok(modules) : Result.fail();
    }

    @GetMapping("getBySystemId")
    public Result<List<Module>> getModulesBySubsystemId(@RequestParam Integer subsystemId) {
        List<Module> modules = moduleService.getModulesBySubsystemId(subsystemId);
        return modules != null && !modules.isEmpty() ? Result.ok(modules) : Result.fail();
    }

    @GetMapping("getModulesByReportId")
    public Result<List<ReportModules>> getModulesByReportId(@RequestParam Integer reportId) {
        List<ReportModules> modules = moduleService.getModulesByReportId(reportId);
        return modules != null && !modules.isEmpty() ? Result.ok(modules) : Result.fail();
    }
    @GetMapping("getBySubId")
    public Result<List<Module>> getModuleBySubId(@RequestParam Integer subId) {
        try {//测试
            if (subId == null || subId <= 0) {
                return Result.fail("ID值无效");
            }
           List<Module> modules = moduleService.getModulesBySubsystemId(subId);
            return modules != null ? Result.ok(modules) : Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

}
